// CSE 142 Winter 2008, Marty Stepp // This program demonstrates some uses of the Math class // and methods that return values. // public class MathExamples { public static void main(String[] args) { // statement - println, call a method, // declaring/assigning a variable // ends with a semicolon; // // expression - a value, or something that computes a value // 2 * 2 // x + 1 // Math.pow(3, 4) System.out.println("hello"); System.out.println(1 + 2 * 3 - 4); // a statement that contains an expression System.out.println(Math.pow(3, 4)); // a statement that contains an expression double grade = 2.6; // grade = Math.random() * 4; // Math.round(grade); grade = Math.round(grade); System.out.println("Your grade in the course is " + grade); System.out.println(Math.random()); System.out.println(Math.random()); // return value - a method computes a result // calling that method is now an expression // whose value is equal to the method's return value // create a variable named lineSlope, // and set it equal to the value returned // from calling slope(0, 0, 5, 10) double lineSlope = slope(0, 0, 5, 10); System.out.println("The slope of the line is " + lineSlope); } // Returns the slope of the line between the given points. public static double slope(int x1, int y1, int x2, int y2) { double dy = y2 - y1; double dx = x2 - x1; double result = dy / dx; return result; } }